import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-08-08
 * Time: 14:38
 * To BE A Better Person-2020-3-2
 */
public class TcpEchoClient {
    private Socket socket=null;
    public TcpEchoClient() throws IOException {
        socket=new Socket("127.0.0.1",8888);
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        try(InputStream inputStream= socket.getInputStream(); OutputStream outputStream= socket.getOutputStream()){
            Scanner scannerNet=new Scanner(inputStream);
            PrintWriter printWriter=new PrintWriter(outputStream);
            while(true){
                //1.从控制台读取用户输入
                System.out.print(">");
                String request=scanner.next();
                //2.将请求发送给服务器
                printWriter.println(request);
                printWriter.flush();
                //3.从服务器读取数据
                String response= scannerNet.next();
                //4.打印结果
                System.out.printf("req: %s;resp: %s\n",request,response);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client=new TcpEchoClient();
        client.start();;
    }
}
